package com.project.webpage.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.project.webpage.dto.SearchByRqParam;
import com.project.webpage.dto.SkbDailyDto;
import com.project.webpage.dto.SkbDetailDto;
import com.project.webpage.mapper.SkbDailyMapper;
import com.project.webpage.service.SkbDailyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SkbDailyServiceImpl implements SkbDailyService {

    @Resource
    private SkbDailyMapper skbDailyMapper;

    @Override
    @DS("master")
    public List<SkbDailyDto> getListByJgxlidForPage(SearchByRqParam param) {
        return skbDailyMapper.selectListByJgxlid(param);
    }

    @Override
    @DS("master")
    public List<SkbDetailDto> getDetailListByJgxlidForPage(SearchByRqParam param) {
        List<SkbDetailDto> skbDetailDtoList = skbDailyMapper.selectDetailListByJgxlid(param);
        if (skbDetailDtoList.size() > 0) {
            Map<Integer, List<SkbDetailDto>> listGroupMap = skbDetailDtoList.stream().collect(Collectors.groupingBy(SkbDetailDto::getSxx_int));
            for (Map.Entry<Integer, List<SkbDetailDto>> entry : listGroupMap.entrySet()) {
                List<SkbDetailDto> list = entry.getValue();
                if (list.size() > 0) {
                    long jhfcTime = 0L;
                    for (int i = 0; i < list.size(); i++) {
                        if (i == 0) {
                            jhfcTime = list.get(i).getJhfcsj_time().getTime();
                        } else {
                            long diffMins = (list.get(i).getJhfcsj_time().getTime() - jhfcTime) / 1000 / 60;
                            list.get(i).setDjg(String.valueOf(diffMins));
                            jhfcTime = list.get(i).getJhfcsj_time().getTime();
                        }
                    }
                }
            }
        }
        return skbDetailDtoList;
    }
}
